package com.dlee.mvc.util;

import java.io.File;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import com.dlee.mvc.annotion.Aspect;
import com.dlee.mvc.annotion.Controller;
import com.dlee.mvc.annotion.Service;

/**
* @author lida
* @since JDK1.7
* @history 2012-7-14 lida
**/
public class FileUtil
{
    /**
     * 取指定包路径下的所有.class文件
     * 
     * @param file 文件
     * @param suffix 后缀
     */
    public static List <File> getAllFile (File file, String suffix,String scanPath)
    {
        List <File> lstFile = new ArrayList <File> ();
        if (file.isDirectory ())
        {
            File[] files = file.listFiles ();
            // 如果是目录
            if (files != null && files.length > 0)
            {
                for (int i = 0; i < files.length; i++)
                {
                    lstFile.addAll (getAllFile (files[i], suffix,scanPath));
                }
            }
        }
        else
        {
            String strPath = file.getAbsolutePath ();
            if (null != suffix)
            {
                // 后缀前的"."号
                int beginSuffix = strPath.lastIndexOf (".");
                String tempSuffix = "";
                if (beginSuffix != -1)
                {// 防止是文件但却没有后缀名结束的文件
                    tempSuffix = strPath.substring (beginSuffix + 1, strPath.length ());
                }
                if (tempSuffix.equals (suffix))
                {
                    try
                    {
                        // 抽取类的路径，再通过反射取类的实例
                        String classFullPath = FileUtil.getClassFullName (file.getPath (), scanPath);
                        Class objClass = Class.forName (classFullPath);
                        if(isHaveAnnotation (objClass,SystemConfig.SCAN_ANNOTATION, scanPath))
                        {
                            lstFile.add (file);
                        }
                    }
                    catch (ClassNotFoundException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
        return lstFile;
    }

    /**
     * 判断是否有指定注解
     * @param file
     * @param scanPath
     * @return
     * @throws ClassNotFoundException
     */
    public static boolean isHaveAnnotation(Class targetClass,Class [] annotationObject,String scanPath) throws ClassNotFoundException
    {
        // 取类的注解类
        Annotation[] classAnnotation = targetClass.getAnnotations ();
        // 当类上存在注解
        if (classAnnotation.length != 0)
        {
            for (Annotation annotation:classAnnotation)
            {
                Object obj = annotation.annotationType ();
                // 如果和已定义的注解是同一个，则可以进行注入
                for(Class anno:annotationObject)
                {
                    if (obj.equals (anno) || obj.equals (anno))
                    {
                        return true;
                    }
                }
                
            }
        }
        return false;
    }
    
    /**
     * 抽取类路径
     * 
     * @param path 路径
     * @param packagePath 包路径
     * @return 路径
     */
    public static String extractPath (String path, String scanPath)
    {
        String paths = path.replace ("\\", ".");
        int i = paths.indexOf (scanPath);
        return paths.substring (i, paths.length ());
    }
    
    public static String getClassFullName(String path,String scanPath)
    {
        String paths = path.replace ("\\", ".");
        int i = paths.indexOf (scanPath);
        String strClassPath =  paths.substring (i, paths.length ());
        //String strClassPath = extractPath (path, scanPath);
        String classFullPath = strClassPath.substring (0, strClassPath.length () - 6);
        return classFullPath;
    }
}
